<?php

/**
 * pollController
 * Manages the admin side of the polls 
 */
class pollController
{
	/**
	 * showAddPage
	 * shows the add form for the poll 
	 */	
	function showAddPage()
	{
		// if the form is submitted, verify and process
		if(isset($_POST['addbutton']))
		{
			$addMsg = $this->verifyPollForm();
		}
		$question = isset($_POST['question']) ? $_POST['question'] : '';
		$answer = array
		(
			isset($_POST['answer'][0]) ? $_POST['answer'][0] : '',
			isset($_POST['answer'][1]) ? $_POST['answer'][1] : '',
			isset($_POST['answer'][2]) ? $_POST['answer'][2] : '',
			isset($_POST['answer'][3]) ? $_POST['answer'][3] : ''
		);
		require ROOT . 'paginas/poll/add.php';
	}

	/**
	 * showEditPage
	 * 
	 * @param int $pollID The ID of the poll that has to be edited
	 * @return boolean returns false if the poll does not exist
	 */
	function showEditPage($pollID)
	{
		$pollQ = mysql_query("SELECT * FROM `poll_vragen` WHERE `id`='" . $pollID . "'");

		if(mysql_num_rows($pollQ) != 1)
		{
			echo "Deze poll bestaat niet!";
			return false;
		}
		$poll = mysql_fetch_assoc($pollQ);

		if(isset($_POST['addbutton']))
		{
			$editMsg = $this->verifyPollForm($poll['id']);
		}
		$question = isset($_POST['question']) ? $_POST['question'] : $poll['vraag'];
		$answer = array
		(
			isset($_POST['answer'][0]) ? $_POST['answer'][0] : $poll['antwoord_1'],
			isset($_POST['answer'][1]) ? $_POST['answer'][1] : $poll['antwoord_2'],
			isset($_POST['answer'][2]) ? $_POST['answer'][2] : $poll['antwoord_3'],
			isset($_POST['answer'][3]) ? $_POST['answer'][3] : $poll['antwoord_4']
		);
		require ROOT . 'paginas/poll/edit.php';
	}

	/**
	 * verifyPollForm
	 * verifies the submitted data by showPollForm
	 * 
	 * @param int $pollID The ID of the poll that's changed (if empty, new poll will be verified)
	 * @return string Errors if exist, else result from savePoll() 
	 */
	private function verifyPollForm($pollID = null)
	{
		$errors = array();

		if(empty($_POST['question']))
		{
			$errors[] = 'U moet een vraag invullen.';
		}

		# poll answers 
		$pollAnswers = array();
		for($i = 0; $i < 4; $i++)
		{
			if(!empty($_POST['answer'][$i]))
			{
				if(!in_array($_POST['answer'][$i], $pollAnswers))
				{
					$pollAnswers[] = $_POST['answer'][$i];
				}
				else
				{
					$errors[] = 'U heeft een dubbel antwoord toegevoegd (' . $_POST['answer'][$i] . ')';
				}
			}
		}

		if(count($pollAnswers) < 2)
		{
			$errors[] = 'U moet minstens 2 poll antwoorden toevoegen.';
		}

		if(count($errors) > 0)
		{
			return '<p class="error"><ul><li>' . implode('</li><li>', $errors) . '</li></ul></p>';
		}
		else
		{
			if(isset($pollID))
			{
				if($_POST['reset'] == 'reset')
				{
					mysql_query("DELETE FROM `poll_antwoorden` WHERE `poll_id`='" . $pollID . "'");
				}

				return $this->savePoll($_POST['question'], $pollAnswers, $pollID);
			}
			else
			{
				return $this->savePoll($_POST['question'], $pollAnswers);
			}
		}
	}

	/**
	 * savePoll
	 * Attempts to save the given data
	 * 
	 * @param string $question The question of the poll
	 * @param array $answers Array of answers
	 * @param int $pollID If provided, the poll with $pollID will be updated, if not a new poll will be created
	 * @return string Result
	 */
	private function savePoll($question, $answers, $pollID = null)
	{
		// Create empty values if they do not exist
		if(!isset($answers[2])){$answers[2] = '';}
		if(!isset($answers[3])){$answers[3] = '';}

		// update poll if $pollID is given, else create new poll
		if(isset($pollID))
		{
			$pollQuery = mysql_query("UPDATE `poll_vragen` SET vraag='" . $question . "', antwoord_1='" . $answers[0] . "', antwoord_2= '" . $answers[1] . "' , antwoord_3='" . $answers[2] . "', antwoord_4='" . $answers[3] . "' WHERE `id`=" . $pollID);
		}
		else
		{
			$pollQuery = mysql_query("INSERT INTO `poll_vragen` (vraag, antwoord_1, antwoord_2, antwoord_3, antwoord_4) VALUES ('" . $question . "', '" . $answers[0] . "', '" . $answers[1] . "' , '" . $answers[2] . "', '" . $answers[3] . "')");
		}

		if($pollQuery)
		{
			if(isset($pollID))
			{
				return '<p class="success">De poll is succesvol gewijzigd!</p>';
			}
			else
			{
				return '<p class="success">De poll is succesvol toegevoegd!</p>';
			}
		}
		else
		{
			return '<p class="error">Er is iets fout gegaan bij het opslaan, probeer het opnieuw.</p>';
		}
	}

	/**
	 * showDeletePage
	 * handles the deletion of a Pol
	 * 
	 * @param int $pollID the ID of the poll to be deleted
	 * @return boolean|string Returns false if poll does not exist, else html is returned
	 */
	function showDeletePage($pollID)
	{
		$pollQuery = mysql_query("SELECT * FROM `poll_vragen` WHERE `id`='" . $pollID . "'");
		
		if(mysql_num_rows($pollQuery) != 1)
		{
			echo "Deze poll bestaat niet.";
			return false;
		}
		
		$poll = mysql_fetch_assoc($pollQuery);
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			mysql_query("DELETE FROM `poll_vragen` WHERE `id`='" . $pollID . "'");
			mysql_query("DELETE FROM `poll_antwoorden` WHERE `poll_id`='" . $pollID . "'");
			echo '<h3>Poll verwijderd</h3><p>De poll met de vraag "' . $poll['vraag'] . '" is succesvol verwijderd.</p>';
		}
		else
		{
			echo '<h3>Bevestig verwijderen</h3>

			<p>Weet u zeker dat u de poll met de vraag "' . $poll['vraag'] . '" wilt verwijderen? Dit kan <strong>niet</strong> ongedaan worden!</p>
			<form method="post"><input type="submit" value="Ja, verwijder deze poll" />&nbsp;<input type="button" onclick="location.href=\'pollbeheer\'" value="Nee, breng mij terug" /></form>
			';
		}
	}
}